package comp.movel.ubiqua.ws;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class ConexaoWebService {
	private static final String URL = "http://www.renanbandeira.com.br/smm/wsdlsmm.php";
	private static final String OPERATION = "buscaPerguntaPorId";
	private static final String NAMESPACE = "urn:smmwsdl";

	public static PerguntaWSDL getPergunta(int id) throws IOException, XmlPullParserException {
		SoapObject request = new SoapObject(NAMESPACE, OPERATION);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		request.addProperty("id", id);
		envelope.addMapping(NAMESPACE, "pergunta",
				new PerguntaWSDL().getClass());
		envelope.setOutputSoapObject(request);
		HttpTransportSE httpTransport = new HttpTransportSE(URL);
		httpTransport.debug = true;
		httpTransport.call("", envelope);
		PerguntaWSDL pergunta = (PerguntaWSDL) envelope.getResponse();
		return pergunta;
	}

	
	public static int qtdPerguntas() {
		SoapObject request = new SoapObject(NAMESPACE, "qtdPerguntas");
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		HttpTransportSE httpTransport = new HttpTransportSE(URL);
		httpTransport.debug = true;
		try {
			httpTransport.call("", envelope);
			int qtd = (Integer) envelope.getResponse();
			return qtd;
		} catch (IOException ioex) {
			ioex.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -2;
		}
	}
}
